//
//  CJNativeCustomEvent.m
//  CJMobileAdapter
//
//  Created by ly on 2024/3/4.
//

#import "CJNativeCustomEvent.h"

@implementation CJNativeCustomEvent

- (instancetype)initWithUnitID:(NSString *)unitID serverInfo:(NSDictionary *)serverInfo localInfo:(NSDictionary *)localInfo {
    if (self = [super initWithUnitID:unitID serverInfo:serverInfo localInfo:localInfo]) {
    }
    return self;
}

- (void)nativeExpressAdDidLoad:(id)nativeExpressAd views:(NSArray *)nativeExpressViews {
    self.adAsset = [[NSMutableDictionary alloc] init];
    [self.adAsset setValue:self forKey:kATAdAssetsCustomEventKey];
    if (nativeExpressViews.count > 0  && [nativeExpressViews.firstObject isKindOfClass:[UIView class]]) {
        UIView *feedView = nativeExpressViews.firstObject;
        [self.adAsset setValue:feedView forKey: kATAdAssetsCustomObjectKey];
        [self.adAsset setValue:self.nativeAd forKey:@"custom-platform-adInstance"];
        [self.adAsset setValue:@(feedView.frame.size.height) forKey:kATNativeADAssetsNativeExpressAdViewHeightKey];
        [self.adAsset setValue:feedView forKey:kATNativeADAssetsMediaViewKey];
        [self.adAsset setValue:@(YES) forKey:kATNativeADAssetsIsExpressAdKey];
    }
    NSDictionary *asset = [NSDictionary dictionaryWithDictionary:self.adAsset];
    [self trackNativeAdLoaded:@[asset]];
        // 防止循环引用 后续优化
    [self.adAsset removeAllObjects];
    self.adAsset = nil;
}

- (void)nativeExpressAdOnShow:(id)nativeExpressView {
    [self trackNativeAdImpression];
}

- (void)nativeExpressAdLoadFailed:(id)nativeExpressAd error:(NSError *)error {
    [self trackNativeAdLoadFailed:error];
}

- (void)nativeExpressAdOnClicked:(id)nativeExpressView {
    [self trackNativeAdClick];
}

- (void)nativeExpressAdOnClosed:(id)nativeExpressView {
    [self trackNativeAdClosed];
}

@end
